package mju.cxf.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mju.cxf.dto.ActivityDTO;
import mju.cxf.entity.Activity;
import mju.cxf.entity.Admin;
import mju.cxf.entity.Categories;
import mju.cxf.mapper.ActivityMapper;
import mju.cxf.service.ActivityService;
import mju.cxf.utils.TimestampUpdaterUtils;
import mju.cxf.vo.ActivityVO;
import mju.cxf.vo.CategoriesVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 广告表 服务实现类
 * </p>
 *
 * @author 郑献
 * @since 2024-10-22
 */

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Autowired
    private PhotoServiceImpl photoServiceImpl;

    /**
     * A分页查询广告信息
     * @param title
     * @return
     */
    @Override
    public Page<ActivityVO> selectPage(String title, String type, Integer status, int current, int size) {
        Page<ActivityVO> page = new Page<>(current, size);
        return this.baseMapper.selectPageVo(page,title, type, status);
    }

    /**
     * A修改广告信息
     * @param activityDTO
     * @return
     */
    @Override
    public boolean updateActivity(ActivityDTO activityDTO) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityDTO, activity);
        LambdaQueryWrapper<Activity> lqw = new LambdaQueryWrapper<>();
        Activity oneActivity = this.getOne(lqw.eq(Activity::getId, activityDTO.getId()));

        LambdaUpdateWrapper<Activity> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper
                .eq(Activity::getId, activity.getId())
                .set(Activity::getImageUrl, activity.getImageUrl());

        if (this.update(activity, updateWrapper)) {
            if (activityDTO.getImageUrl() == null || !(Objects.equals(oneActivity.getImageUrl(), activityDTO.getImageUrl()))) {
                photoServiceImpl.deleteUrl(oneActivity.getImageUrl());
                photoServiceImpl.saveImages(activity.getImageUrl());
            }
            return true;
        }
        return false;
    }

    /**
     * A新增广告信息
     * @param activityDTO
     * @return
     */
    @Override
    public boolean saveActivity(ActivityDTO activityDTO) {
        Activity activity = new Activity();
        activityDTO.setStatus(1);
        activityDTO.setCreateTime(LocalDateTime.now());
        BeanUtils.copyProperties(activityDTO,activity);
        return this.save(activity); // 使用 MyBatis-Plus 提供的 save() 方法
    }

    /**
     * A删除广告信息
     * @param id
     * @return
     */
    @Override
    public boolean deleteActivity(Integer id) {
        return this.removeById(id);
    }
}
